package likou.jindian;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/05/20 15:11
 */
public class _16_20 {
    int l;
    // 建立字母对数字的映射
    int[] map = new int[]{0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,5,6,6,6,7,7,7,7};

    public List<String> getValidT9Words(String num, String[] words) {
        l = num.length();
        int[] nums = new int[l];
        // 为减少计算，把num字符串转换成int数组
        for(int i = 0; i < l; i++){
            nums[i] = num.charAt(i) - '2';
        }

        List<String> res = new ArrayList<>();
        for(String word : words){
            if(isValid(nums, word)){
                res.add(word);
            }
        }
        return res;
    }
    // 根据映射判断单词是否合法
    private boolean isValid(int[] nums, String word){
        for(int i = 0; i < l; i++){
            if(map[word.charAt(i) - 'a'] != nums[i]){
                return false;
            }
        }
        return true;
    }
}
